package extendedEuclid;

import java.util.Random;
import java.util.Scanner;

import aseCrypto.AseInteger;
import aseCrypto.StopWatch;



public class ExtendedEuclid {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Scanner in = new Scanner(System.in);
        
		
		do{
			System.out.println("Method for calculating the gcd:");
			System.out.println("Euclid iterative: enter i");
			System.out.println("Euclid recursive: enter r");
			System.out.println("Bezout iterative: enter b");
			System.out.println("Bezout recursive: enter s");
			System.out.println("Prime factorization: enter p");
			System.out.println("Exit program: enter e");
                        
			final String selected = in.nextLine();
			
			switch(selected){
			case "i":
			case "r":
			case "p":
			case "b":
			case "s":
                            
				AseInteger num1;
				AseInteger num2;
                                
				System.out.println("How many bits shall your numbers be made of (0 for setting number explicitely)?");
				Integer bits = new Integer(in.nextLine());
				
                                
				if(bits.intValue() > 0){
					num1 = new AseInteger(bits, new Random());
					num1 = num1.setBit(bits.intValue()-1);
                                        
					System.out.println("Your first number is: " + num1);
					
					num2 = new AseInteger(bits, new Random());
					num2 = num2.setBit(bits.intValue()-1);
					System.out.println("Your second number is: " + num2);
                                        
				} else {
                                    
					System.out.println("Please input the first integer:");
					String number = in.nextLine();
                                        
			        num1 = new AseInteger(number);
                                
			        System.out.println("Please input the second integer:");
                                
			        number = in.nextLine();
			        num2 = new AseInteger(number);
                                
				}
							
		        AseInteger gcd = AseInteger.ONE;
		        AseInteger[] bez=null;
		        StopWatch watch = new StopWatch();
		        
		        watch.start();
                        
		        switch(selected){
		        case "i":
		        	gcd = num1.getGcd(num2);
		            break;
		        case "r":
		        	gcd = num1.getGcdRec(num2);
		        	break;		        	
				case "p":
					gcd = num1.getGcdFact(num2);
					break;
				case "b":
		        	 bez = num1.getBezout(num2);
		            break;
		        case "s":
		        	bez = num1.getBezoutRec(num2);
		        	break;		        
		        }
		        watch.stop();
		 
		        switch(selected)
		        {
		        case "i":
		        case "r":
		        case "p":
		        	System.out.println("\nThe gcd of " + num1 + "\nand " + num2 + "\nis " + gcd + "\ncalculated in " + watch.getElapsedTime() + " ms \n\n");
		        	break;
		        	
		        case "b":
		        case "s":
		        	System.out.println("\n" + bez[2] + "\n= " + num1 + "\n* " + bez[0] + "\n+ " + num2 + "\n* " + bez[1]  + "\nin " + watch.getElapsedTime() + " ms \n\n");
		        	break;
		        }
		        break;
		        
			case "e":
				System.out.println("Goodbye...");
				in.close();
				return;
				
			default:
				System.out.println("\nWrong input...\n\n");		
			}
		
		} while(true);
	}

}
